/*///////////////////////////////////////////////////////////

M48T02-150PC1_TIMEKEEPER@RAM
16 Kbit (2Kb x8) TIMEKEEPER@ SRAM

  *INTEGRATED ULTRA LOW POWER SRAM,REAL TIME CLOCK and POWER-FAIL CONTROLCIRCUIT
  *BYTEWIDE RAM-LIKECLOCK ACCESS
  *BCD CODED YEAR, MONTH, DAY, DATE,HOURS, MINUTES and SECONDS
  *TYPICAL CLOCK ACCURACY of ± 1 MINUTE a MONTH, AT 25°C
  *SOFTWARE CONTROLLED CLOCK CALIBRATION for HIGH ACCURACY APPLICATIONS
  *AUTOMATICPOWER-FAILCHIPDESELECTand WRITEPROTECTION
  *WRITE PROTECT VOLTAGES(VPFD = Power-fail Deselect Voltage):
  *– M48T02: 4.5V ≤ VPFD ≤ 4.75V 
  *– M48T12: 4.2V ≤ VPFD ≤ 4.5V
  *SELF-CONTAINED BATTERY and CRYSTAL in the CAPHAT DIP PACKAGE
  *PIN and FUNCTION COMPATIBLE with JEDEC STANDARD 2Kb x8 SRAMs

2021-06-30


//////////////////////////////////////////////////////////*/
/*
const int Addr0  = 22;  //Address Inputs
const int Addr1  = 23;
const int Addr2  = 24;
const int Addr3  = 25;
const int Addr4  = 26;
const int Addr5  = 27;
const int Addr6  = 28;
const int Addr7  = 29;
const int Addr8  = 30;
const int Addr9  = 31;
const int Addr10 = 32;

const int D0  = 49;  //Data Inputs / Outputs :PINL
const int D1  = 48;
const int D2  = 47;
const int D3  = 46;
const int D4  = 45;
const int D5  = 44;
const int D6  = 43;
const int D7  = 42;
*/
/*////////////////////////////////////////////////////////////////////////////////
 _______________________________________________________________________________
|           |               Data                             | Function/Range  |
|  Address  |------------------------------------------------|-----------------|
|           |  D7 |  D6 |  D5 |  D4 |  D3 |  D2 |  D1 |  D0  |    BCD Format   |
|-----------|-----|-----|-----|-----|------------------------|-----------------|
|  7FFh     |        10 Years       |        Year            |  Year    00-99  |
|-----------|-----|-----|-----|-----|------------------------|-----------------|
|  7FEh     |  0  |  0  |  0  | 10M.|        Month           |  Month   01-12  |
|-----------|-----|-----|-----------|------------------------|-----------------|
|  7FDh     |  0  |  0  |   10 Date |        Date            |  Date    01-31  |
|-----------|-----|-----|-----------|------------------------|-----------------|
|  7FCh     |  0  |  FT |  0  |  0  |  0  |  Date            |  Date    01-31  |
|-----------|-----|-----|-----------|------------------------|-----------------|
|  7FBh     |  0  |  0  |  10 Hours |        Hours           |  Hour    00-23  |
|-----------|-----|-----------------|------------------------|-----------------|
|  7FAh     |  0  |     1 Minutes   |        Minutes         |  Minutes 00-59  |
|-----------|-----|-----------------|------------------------|-----------------|
|  7F9h     |  ST |    1  Seconds   |        Seconds         |  Seconds 00-59  |
|-----------|-----|----- ------------------------------------|-----------------|
|  7F8h     |  W  |  R  |  S  |    Calibration               |   Control       |
|___________|_____|_____|_____|______________________________|_________________|
              S = SIGN Bit
              FT = FREQUENCY TEST Bit (Set to ’0’ for normal clock operation)
              R = READ Bit
              W = WRITE Bit
              ST = STOP Bit
              0 = Must be set to ’0’
////////////////////////////////////////////////////////////////////////////////*/

const int Data[8]={42,43,44,45,46,47,48,49};          //D0............D7
const int Addr[11]={22,23,24,25,26,27,28,29,30,31,32};//A0............A10    

const int  E  = 33;   // Chip Enable   -18
const int  G  = 34;   // Output Enable -20
const int  W  = 35;   // Write Enable  -21

const int Year        = 0x7FF;
const int Month       = 0x7FE;
const int Date        = 0x7FD;
const int Day         = 0x7FC;
const int Hour        = 0x7FB;
const int Minute      = 0x7FA;
const int Second      = 0x7F9;
const int Control     = 0x7F8;

const int FT_Bit      = 0X40 ;                    //FREQUENCY TEST Bit 
const int ST_Bit      = 0X80 ;                    //STOP  Bit
const int W_Bit       = 0X80 ;                    //WRITE Bit
const int R_Bit       = 0X40 ;                    //READ  Bit
const int S_Bit       = 0X20 ;                    //SIGN  Bit

void setup() {
  Serial.begin(115200);
  Serial.println("Serial port");
  M48T02_PinConfig();
  Serial.println("M48T02_PinConfig");
}

void Data_bus_out(){
  for(uint8_t i=0;i<8;i++)
  {
    pinMode(Data[i], OUTPUT);
  }
}

void Data_bus_in(){
  for(uint8_t i=0;i<8;i++)
  {
    pinMode(Data[i], INPUT);
  }
}

uint8_t M48T02_PinConfig(void){                   //程序可用
  /*for(int i=42;i<50;i++){//Data Inputs / Outputs
    pinMode(i, INPUT);//OUTPUT  INPUT
  }*/
  for(int i=22;i<33;i++){//Address Port
    pinMode(i, OUTPUT);
  }
  pinMode(E, OUTPUT);
  pinMode(G, OUTPUT);
  pinMode(W, OUTPUT);
  return 0;
}

uint8_t Read_Data(void){                          //程序可用
    /*uint8_t R_Data;
    R_Data = 0;
    R_Data |= (digitalRead(D0)  << 0);
    R_Data |= (digitalRead(D1)  << 1);
    R_Data |= (digitalRead(D2)  << 2);
    R_Data |= (digitalRead(D3)  << 3);
    R_Data |= (digitalRead(D4)  << 4);
    R_Data |= (digitalRead(D5)  << 5);
    R_Data |= (digitalRead(D6)  << 6);
    R_Data |= (digitalRead(D7)  << 7);
    return R_Data;*/
  Data_bus_in();
  for(uint8_t i=0;i<8;i++){digitalWrite(Data[i],1);}
  uint8_t readb = digitalRead(Data[0])|(digitalRead(Data[1])<<1)
            |(digitalRead(Data[2])<<2)|(digitalRead(Data[3])<<3)
            |(digitalRead(Data[4])<<4)|(digitalRead(Data[5])<<5)
            |(digitalRead(Data[6])<<6)|(digitalRead(Data[7])<<7);
  return readb;
}

uint8_t Write_Data(uint8_t W_Data){               //程序可用
    Data_bus_out();
    //uint8_t W_Data;
    //W_Data = 0;
    /* 
    digitalWrite(D0, (W_Data >> 0)&0X01);  
    digitalWrite(D1, (W_Data >> 1)&0X01);  
    digitalWrite(D2, (W_Data >> 2)&0X01);  
    digitalWrite(D3, (W_Data >> 3)&0X01);  
    digitalWrite(D4, (W_Data >> 4)&0X01);  
    digitalWrite(D5, (W_Data >> 5)&0X01);  
    digitalWrite(D6, (W_Data >> 6)&0X01);  
    digitalWrite(D7, (W_Data >> 7)&0X01);  
    return 0;*/
  for(uint8_t i=0;i<8;i++)
  {digitalWrite(Data[i],bitRead(W_Data,i));}
}

uint16_t Write_Addr(uint16_t W_Addr){             //程序可用
    //uint8_t W_Data;
    //W_Data = 0;
    /*
     * 
    digitalWrite(Addr0 ,  (W_Addr >> 0  )&0X01);  
    digitalWrite(Addr1 ,  (W_Addr >> 1  )&0X01);  
    digitalWrite(Addr2 ,  (W_Addr >> 2  )&0X01);  
    digitalWrite(Addr3 ,  (W_Addr >> 3  )&0X01);  
    digitalWrite(Addr4 ,  (W_Addr >> 4  )&0X01);  
    digitalWrite(Addr5 ,  (W_Addr >> 5  )&0X01);  
    digitalWrite(Addr6 ,  (W_Addr >> 6  )&0X01);  
    digitalWrite(Addr7 ,  (W_Addr >> 7  )&0X01);  
    digitalWrite(Addr8 ,  (W_Addr >> 8  )&0X01);  
    digitalWrite(Addr9 ,  (W_Addr >> 9  )&0X01);  
    digitalWrite(Addr10,  (W_Addr >> 10 )&0X01);  
    return 0; */
    
  for(uint8_t i=0;i<11;i++)
  {digitalWrite(Addr[i],bitRead(W_Addr,i));}  
}

uint16_t Read_Byte(uint16_t Address){             //程序可用
    digitalWrite(W,HIGH);  
    digitalWrite(G,HIGH);  
    digitalWrite(E,LOW);  
    Write_Addr(Address);
    digitalWrite(G,LOW);  
    digitalWrite(W,HIGH);
    uint8_t data = Read_Data();
    digitalWrite(E,HIGH);  
    digitalWrite(W,LOW);  
    return data;
}

uint16_t Write_Byte(uint16_t Address,uint8_t Data){
    digitalWrite(W,HIGH);  
    digitalWrite(G,HIGH);  
    digitalWrite(E,HIGH);  
    Write_Addr(Address);
    digitalWrite(W,LOW);  
    digitalWrite(E,LOW);  
    Write_Data(Data);
    digitalWrite(E,HIGH);  
    digitalWrite(W,HIGH);  
    return Data;
}

uint8_t Read_Test(void){                          //程序可用
   for(uint16_t i=2040;i<2048;i++){               //测试程序，Read & Write
    Serial.print(" Read:");
    Serial.print(i,HEX);
    Serial.print(" = ");
    Serial.print(Read_Byte(i),HEX);//读测试  
  }
    Serial.print("  ");
    Serial.print(Read_Byte(Second),BIN);
    Serial.println("  ");
}

uint8_t Write_Test(void){                         //程序可用
  for(uint16_t i=0;i<2040;i++){                   //测试程序，Read & Write
    Serial.print("Write_Data:");
    Serial.print(" ");
    Serial.print(i,HEX);
    Serial.print(" = "); 
    Serial.println(Write_Byte(i,0Xff),HEX);       //写测试
  }
   Serial.print(Write_Byte(Control,Read_Byte(Control)| W_Bit),BIN);             //准备写入停止刷新
   //Serial.print(Write_Byte(Control,Read_Byte(Control)*~W_Bit),HEX);             //写秒
   //Serial.print(Write_Byte(Second ,Read_Byte(Second) || ST_Bit),HEX);           //写秒

   Serial.print(Write_Byte(Minute ,0X21),HEX);    //写分钟
   Serial.print(Write_Byte(Hour   ,0X18),HEX);    //写小时
   Serial.print(Write_Byte(Day    ,0X4),HEX);     //写星期
   Serial.print(Write_Byte(Date   ,0X1),HEX);     //写日期
   Serial.print(Write_Byte(Month  ,0X7),HEX);     //写月份
   Serial.println(Write_Byte(Year ,0X21),HEX);    //写年份

   Serial.print(Write_Byte(Control,Read_Byte(Control)*!W_Bit),HEX);             //写入完成恢复刷新
}

uint8_t Test(uint8_t range){
  switch (range) {
    case 0:    // your hand is on the sensor
        Serial.print(Write_Byte(Control,Read_Byte(Control)| W_Bit),BIN);             //准备写入停止刷新
        Write_Test();
        Serial.print(Write_Byte(Control,Read_Byte(Control)*!W_Bit),HEX);             //写入完成恢复刷新
      break;
    case 1:    // your hand is close to the sensor

        Serial.print(Write_Byte(Control,Read_Byte(Control)| R_Bit),BIN);             //准备读取停止刷新
        Read_Test();
        Serial.print(Write_Byte(Control,Read_Byte(Control)*!R_Bit),HEX);             //读取完成恢复刷新
      break;

   }
 
}
   //Serial.print(Write_Byte(Control,Read_Byte(Control)| R_Bit),BIN);             //准备读取停止刷新
   //Serial.print(Write_Byte(Control,Read_Byte(Control)*!R_Bit),HEX);             //读取完成恢复刷新
   //Serial.print(Write_Byte(Control,Read_Byte(Control)| W_Bit),BIN);             //准备写入停止刷新
   //Serial.print(Write_Byte(Control,Read_Byte(Control)*!W_Bit),HEX);             //写入完成恢复刷新
   //Serial.print(Write_Byte(Second,Read_Byte(Second)*!ST_Bit),HEX);              //启动OSC
   //Serial.print(Write_Byte(Second,Read_Byte(Second)| ST_Bit),HEX);              //停止OSC
void loop() {
  //Test(1);
   Serial.println(Write_Byte(Second,Read_Byte(Second)*!ST_Bit),HEX);              //停止OSC
     //   Serial.print(Write_Byte(Control,Read_Byte(Control)| W_Bit),HEX);             //准备写入停止刷新
    //Serial.print(" ");
      //  Serial.print(Write_Byte(Day,Read_Byte(Day)| FT_Bit),BIN);             // 
    //Serial.print(" ");
      //  Serial.print(Write_Byte(Control,Read_Byte(Control)*!W_Bit),HEX);             //写入完成恢复刷新
    //Serial.print(" ");
     //   Serial.print(Read_Byte(Day),BIN);
        //Write_Addr(Second);
  //while(1);
}



/*uint8_t Read(void){                             //程序可用
    return PINL;//D0-D7:49-42
}*/

/*uint8_t Write(uint8_t W_Data){                  //该程序有错误。。。
    //PINL = W_Data;//D0-D7:49-42
    PINL = 4;//D0-D7:49-42
    return 0;
    //return PINL;//D0-D7:49-42
}
*/

//
